package View;

import java.util.*;
import java.awt.event.ActionEvent;
import javax.swing.*;
import Model.*;
import Controller.*;
import javax.swing.JFileChooser;
import javax.swing.filechooser.FileFilter;
import javax.swing.filechooser.FileNameExtensionFilter;

/**
 * Program's main window
 *
 * @author Jose Filipe M Santos - 7277502
 * @author Matheus Martins Teixeira - 7277482
 */
public class Window extends JFrame {

//Closing window handler
    private class windowlistener implements java.awt.event.WindowListener {

	@Override
	public void windowClosing(java.awt.event.WindowEvent arg0) {
	    QuitActionPerformed();
	}

	@Override
	public void windowOpened(java.awt.event.WindowEvent arg0) {
	    LoadActionPerformed();
	}

	@Override
	public void windowClosed(java.awt.event.WindowEvent arg0) {
	}

	@Override
	public void windowIconified(java.awt.event.WindowEvent arg0) {
	}

	@Override
	public void windowDeiconified(java.awt.event.WindowEvent arg0) {
	}

	@Override
	public void windowActivated(java.awt.event.WindowEvent arg0) {
	}

	@Override
	public void windowDeactivated(java.awt.event.WindowEvent arg0) {
	}
    }
//Menubar
    private JMenuBar menubar;
//Save, Load , View and Quit menus
    private JMenu Conn;
    private JMenu File;
    private JMenu Help;
    private JMenu View;
    private JMenu Multi;
    private JMenuItem MultiGame;
    private JMenuItem Mode;
    private JMenuItem Server;
    private JMenuItem Client;
    private JMenuItem Save;
    private JMenuItem Load;
    private JMenuItem Quit;
    private JMenuItem About;
    private JMenuItem Ht1;
    private JMenuItem Ht2;
//Labels
    private JLabel TitleLabel;
    private JLabel Team1Label;
    private JLabel Team2Label;
//Buttons to scale and simulate
    private JButton ScaleT1;
    private JButton ScaleT2;
    private JButton Simulate;
//Checkboxes refering to player in team 1
    private JCheckBox Pl1T1;
    private JCheckBox Pl2T1;
    private JCheckBox Pl3T1;
    private JCheckBox Pl4T1;
    private JCheckBox Pl5T1;
    private JCheckBox Pl6T1;
    private JCheckBox Pl7T1;
    private JCheckBox Pl8T1;
    private JCheckBox Pl9T1;
    private JCheckBox Pl10T1;
//CheckBoxes refering to player in team 2
    private JCheckBox Pl1T2;
    private JCheckBox Pl2T2;
    private JCheckBox Pl3T2;
    private JCheckBox Pl4T2;
    private JCheckBox Pl5T2;
    private JCheckBox Pl6T2;
    private JCheckBox Pl7T2;
    private JCheckBox Pl8T2;
    private JCheckBox Pl9T2;
    private JCheckBox Pl10T2;
//Teams panels
    private JPanel PanelTeam1;
    private JPanel PanelTeam2;
    private JPanel Panel3;
//Game Data to operate
// Client/Server
    private boolean multiplayer;
    private boolean isServer;
    private Server server;
    private Client client;
    private GameData G;
    public Game Match;
    private Thread t1, t2;

//Begining of methods
    /**
     * Main window constructor
     *
     * @param Gamedata Game data loaded from file
     */
    public Window(GameData Gamedata) {

	super(Gamedata.getLoadedTeam1().getName() + " x " + Gamedata.getLoadedTeam2().getName() + " - Championship Simulator");
	this.Match = new Model.Game(new java.util.Date(), Gamedata.getLoadedTeam1(), Gamedata.getLoadedTeam2());
	this.G = Gamedata;
	TitleLabel = new JLabel("Championship Simulator");
	TitleLabel.setSize(800, 50);
	TitleLabel.setLocation(0, 0);
	TitleLabel.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
	Team1Label = new JLabel(Match.getHomeTeam().getName());
	Team1Label.setSize(250, 50);
	Team1Label.setLocation(50, 50);
	Team1Label.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
	Team2Label = new JLabel(Match.getVisitorTeam().getName());
	Team2Label.setSize(250, 50);
	Team2Label.setLocation(450, 50);
	Team2Label.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);

	ScaleT1 = new JButton("Scale");
	ScaleT1.setSize(100, 30);
	ScaleT1.setLocation(125, 400);
	ScaleT1.addActionListener(new java.awt.event.ActionListener() {

	    @Override
	    public void actionPerformed(ActionEvent e) {
		ScaleT1ActionPerformed();
	    }
	});
	ScaleT1.setEnabled(false);
	ScaleT1.setToolTipText("Click here to select th current players");

	ScaleT2 = new JButton("Scale");
	ScaleT2.setSize(100, 30);
	ScaleT2.setLocation(550, 400);
	ScaleT2.addActionListener(new java.awt.event.ActionListener() {

	    @Override
	    public void actionPerformed(ActionEvent e) {
		ScaleT2ActionPerformed();
	    }
	});
	ScaleT2.setEnabled(false);
	ScaleT2.setToolTipText("Click here to select the current players");


	Simulate = new JButton("Simulate");
	Simulate.setSize(120, 35);
	Simulate.setLocation(340, 460);
	Simulate.addActionListener(new java.awt.event.ActionListener() {

	    @Override
	    public void actionPerformed(ActionEvent e) {
		SimulateActionPerformed();
	    }
	});
	Simulate.setEnabled(false);
	Simulate.setToolTipText("Click here to simulate the game");

	menubar = new JMenuBar();

	Conn = new JMenu("Connection");
	File = new JMenu("File");
	Help = new JMenu("Help");
	View = new JMenu("View");
	Mode = new JMenuItem("Game mode...");
	Server = new JMenuItem("Start server");
	Client = new JMenuItem("Connect...");
	Multi = new JMenu("Multi");
	MultiGame = new JMenuItem("MultiGame");
	MultiGame.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_M, java.awt.event.InputEvent.CTRL_MASK));
	Quit = new JMenuItem("Quit");
	Quit.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_Q, java.awt.event.InputEvent.CTRL_MASK));
	Load = new JMenuItem("Load");
	Load.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_L, java.awt.event.InputEvent.CTRL_MASK));
	Save = new JMenuItem("Save");
	Save.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_S, java.awt.event.InputEvent.CTRL_MASK));
	About = new JMenuItem("About");
	About.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_F1, 0));
	Ht1 = new JMenuItem("View Team 1 History");
	Ht1.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_F11, 0));
	Ht2 = new JMenuItem("View Team 2 History");
	Ht2.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_F12, 0));


	Conn.add(Server);
	Conn.add(Client);
	File.add(Mode);
	File.add(Load);
	File.add(Save);
	File.add(Quit);
	Multi.add(MultiGame);
	Help.add(About);
	View.add(Ht1);
	View.add(Ht2);

	MultiGame.addActionListener(new java.awt.event.ActionListener() {

	    @Override
	    public void actionPerformed(ActionEvent e) {
		MultipleModeWindow multi = new MultipleModeWindow();
	    }
	});
	About.addActionListener(new java.awt.event.ActionListener() {

	    @Override
	    public void actionPerformed(ActionEvent e) {
		menuAboutActionPerformed();
	    }
	});

	Load.addActionListener(new java.awt.event.ActionListener() {

	    @Override
	    public void actionPerformed(ActionEvent e) {
		LoadActionPerformed();
	    }
	});

	Save.addActionListener(new java.awt.event.ActionListener() {

	    @Override
	    public void actionPerformed(ActionEvent e) {
		SaveActionPerformed();
	    }
	});

	Quit.addActionListener(new java.awt.event.ActionListener() {

	    @Override
	    public void actionPerformed(ActionEvent e) {
		QuitActionPerformed();
	    }
	});

	Ht1.addActionListener(new java.awt.event.ActionListener() {

	    @Override
	    public void actionPerformed(ActionEvent e) {
		Ht1ActionPerformed();
	    }
	});

	Ht2.addActionListener(new java.awt.event.ActionListener() {

	    @Override
	    public void actionPerformed(ActionEvent e) {
		Ht2ActionPerformed();
	    }
	});
	Mode.addActionListener(new java.awt.event.ActionListener() {

	    @Override
	    public void actionPerformed(ActionEvent e) {
		selectModeActionPerformed();
	    }
	});

	Server.addActionListener(new java.awt.event.ActionListener() {

	    @Override
	    public void actionPerformed(ActionEvent e) {
		menuServerActionPerformed();
	    }
	});

	Client.addActionListener(new java.awt.event.ActionListener() {

	    @Override
	    public void actionPerformed(ActionEvent e) {
		menuClientActionPerformed();
	    }
	});


	menubar.add(File);
	menubar.add(Multi);
	menubar.add(View);
	menubar.add(Help);


// G variable contains the game data, from wich we get the loaded team, from wich we get a list of players from wich we get a player, from wich we get the atributtes
// the chain is: Game data-> Team loaded -> List of players -> Player -> Atributtes

//Ckeckboxes refering to team 1


	Pl1T1 = new JCheckBox(Match.getHomeTeam().getGkPlayers().get(0).getName() + "(Gk, " + Match.getHomeTeam().getGkPlayers().get(0).getHability() + ")" + " [0]");
	Pl1T1.setSize(200, 25);
	Pl1T1.setLocation(55, 105);
	Pl1T1.addActionListener(new java.awt.event.ActionListener() {

	    @Override
	    public void actionPerformed(ActionEvent e) {
		Match.getHomeTeam().ChangeStatus(0);
		if (Match.getHomeTeam().isReady()) {
		    ScaleT1.setEnabled(true);
		} else if ((Match.getHomeTeam().isReady() == false) && ScaleT1.isEnabled() == true) {
		    ScaleT1.setEnabled(false);
		}
	    }
	});
	Pl2T1 = new JCheckBox(Match.getHomeTeam().getGkPlayers().get(1).getName() + "(Gk, " + Match.getHomeTeam().getGkPlayers().get(1).getHability() + ")" + " [0]");
	Pl2T1.setSize(200, 25);
	Pl2T1.setLocation(55, 130);
	Pl2T1.addActionListener(new java.awt.event.ActionListener() {

	    @Override
	    public void actionPerformed(ActionEvent e) {
		Match.getHomeTeam().ChangeStatus(1);
		if (Match.getHomeTeam().isReady()) {
		    ScaleT1.setEnabled(true);
		} else if ((Match.getHomeTeam().isReady() == false) && ScaleT1.isEnabled() == true) {
		    ScaleT1.setEnabled(false);
		}
	    }
	});
	Pl3T1 = new JCheckBox(Match.getHomeTeam().getAttackPlayers().get(0).getName() + "(Fwd, " + Match.getHomeTeam().getAttackPlayers().get(0).getHability() + ")" + " [" + ((PlayerAt) Match.getHomeTeam().getAttackPlayers().get(0)).getGoals() + "]");
	Pl3T1.setSize(200, 25);
	Pl3T1.setLocation(55, 155);
	Pl3T1.addActionListener(new java.awt.event.ActionListener() {

	    @Override
	    public void actionPerformed(ActionEvent e) {
		Match.getHomeTeam().ChangeStatus(2);
		if (Match.getHomeTeam().isReady()) {
		    ScaleT1.setEnabled(true);
		} else if ((Match.getHomeTeam().isReady() == false) && ScaleT1.isEnabled() == true) {
		    ScaleT1.setEnabled(false);
		}
	    }
	});
	Pl4T1 = new JCheckBox(Match.getHomeTeam().getAttackPlayers().get(1).getName() + "(Fwd, " + Match.getHomeTeam().getAttackPlayers().get(1).getHability() + ")" + " [" + ((PlayerAt) Match.getHomeTeam().getAttackPlayers().get(1)).getGoals() + "]");
	Pl4T1.setSize(200, 25);
	Pl4T1.setLocation(55, 180);
	Pl4T1.addActionListener(new java.awt.event.ActionListener() {

	    @Override
	    public void actionPerformed(ActionEvent e) {
		Match.getHomeTeam().ChangeStatus(3);
		if (Match.getHomeTeam().isReady()) {
		    ScaleT1.setEnabled(true);
		} else if ((Match.getHomeTeam().isReady() == false) && ScaleT1.isEnabled() == true) {
		    ScaleT1.setEnabled(false);
		}
	    }
	});
	Pl5T1 = new JCheckBox(Match.getHomeTeam().getAttackPlayers().get(2).getName() + "(Fwd, " + Match.getHomeTeam().getAttackPlayers().get(2).getHability() + ")" + " [" + ((PlayerAt) Match.getHomeTeam().getAttackPlayers().get(2)).getGoals() + "]");
	Pl5T1.setSize(200, 25);
	Pl5T1.setLocation(55, 205);
	Pl5T1.addActionListener(new java.awt.event.ActionListener() {

	    @Override
	    public void actionPerformed(ActionEvent e) {
		Match.getHomeTeam().ChangeStatus(4);
		if (Match.getHomeTeam().isReady()) {
		    ScaleT1.setEnabled(true);
		} else if ((Match.getHomeTeam().isReady() == false) && ScaleT1.isEnabled() == true) {
		    ScaleT1.setEnabled(false);
		}
	    }
	});
	Pl6T1 = new JCheckBox(Match.getHomeTeam().getAttackPlayers().get(3).getName() + "(Fwd, " + Match.getHomeTeam().getAttackPlayers().get(3).getHability() + ")" + " [" + ((PlayerAt) Match.getHomeTeam().getAttackPlayers().get(3)).getGoals() + "]");
	Pl6T1.setSize(200, 25);
	Pl6T1.setLocation(55, 230);
	Pl6T1.addActionListener(new java.awt.event.ActionListener() {

	    @Override
	    public void actionPerformed(ActionEvent e) {
		Match.getHomeTeam().ChangeStatus(5);
		if (Match.getHomeTeam().isReady()) {
		    ScaleT1.setEnabled(true);
		} else if ((Match.getHomeTeam().isReady() == false) && ScaleT1.isEnabled() == true) {
		    ScaleT1.setEnabled(false);
		}
	    }
	});
	Pl7T1 = new JCheckBox(Match.getHomeTeam().getDefensePlayers().get(0).getName() + "(Def, " + Match.getHomeTeam().getDefensePlayers().get(0).getHability() + ")" + " [" + ((PlayerDef) Match.getHomeTeam().getDefensePlayers().get(0)).getGoals() + "]");
	Pl7T1.setSize(200, 25);
	Pl7T1.setLocation(55, 255);
	Pl7T1.addActionListener(new java.awt.event.ActionListener() {

	    @Override
	    public void actionPerformed(ActionEvent e) {
		Match.getHomeTeam().ChangeStatus(6);
		if (Match.getHomeTeam().isReady()) {
		    ScaleT1.setEnabled(true);
		} else if ((Match.getHomeTeam().isReady() == false) && ScaleT1.isEnabled() == true) {
		    ScaleT1.setEnabled(false);
		}
	    }
	});
	Pl8T1 = new JCheckBox(Match.getHomeTeam().getDefensePlayers().get(1).getName() + "(Def, " + Match.getHomeTeam().getDefensePlayers().get(1).getHability() + ")" + " [" + ((PlayerDef) Match.getHomeTeam().getDefensePlayers().get(1)).getGoals() + "]");
	Pl8T1.setSize(200, 25);
	Pl8T1.setLocation(55, 280);
	Pl8T1.addActionListener(new java.awt.event.ActionListener() {

	    @Override
	    public void actionPerformed(ActionEvent e) {
		Match.getHomeTeam().ChangeStatus(7);
		if (Match.getHomeTeam().isReady()) {
		    ScaleT1.setEnabled(true);
		} else if ((Match.getHomeTeam().isReady() == false) && ScaleT1.isEnabled() == true) {
		    ScaleT1.setEnabled(false);
		}
	    }
	});
	Pl9T1 = new JCheckBox(Match.getHomeTeam().getDefensePlayers().get(2).getName() + "(Def, " + Match.getHomeTeam().getDefensePlayers().get(2).getHability() + ")" + " [" + ((PlayerDef) Match.getHomeTeam().getDefensePlayers().get(2)).getGoals() + "]");
	Pl9T1.setSize(200, 25);
	Pl9T1.setLocation(55, 305);
	Pl9T1.addActionListener(new java.awt.event.ActionListener() {

	    @Override
	    public void actionPerformed(ActionEvent e) {
		Match.getHomeTeam().ChangeStatus(8);
		if (Match.getHomeTeam().isReady()) {
		    ScaleT1.setEnabled(true);
		} else if ((Match.getHomeTeam().isReady() == false) && ScaleT1.isEnabled() == true) {
		    ScaleT1.setEnabled(false);
		}
	    }
	});
	Pl10T1 = new JCheckBox(Match.getHomeTeam().getDefensePlayers().get(3).getName() + "(Def, " + Match.getHomeTeam().getDefensePlayers().get(3).getHability() + ")" + " [" + ((PlayerDef) Match.getHomeTeam().getDefensePlayers().get(3)).getGoals() + "]");
	Pl10T1.setSize(200, 25);
	Pl10T1.setLocation(55, 330);
	Pl10T1.addActionListener(new java.awt.event.ActionListener() {

	    @Override
	    public void actionPerformed(ActionEvent e) {
		Match.getHomeTeam().ChangeStatus(9);
		if (Match.getHomeTeam().isReady()) {
		    ScaleT1.setEnabled(true);
		} else if ((Match.getHomeTeam().isReady() == false) && ScaleT1.isEnabled() == true) {
		    ScaleT1.setEnabled(false);
		}
	    }
	});
//CheckBoxes refering to team 2

	Pl1T2 = new JCheckBox(Match.getVisitorTeam().getGkPlayers().get(0).getName() + "(Gk, " + Match.getVisitorTeam().getGkPlayers().get(0).getHability() + ")" + " [0]");
	Pl1T2.setSize(200, 25);
	Pl1T2.setLocation(455, 105);
	Pl1T2.addActionListener(new java.awt.event.ActionListener() {

	    @Override
	    public void actionPerformed(ActionEvent e) {
		Match.getVisitorTeam().ChangeStatus(0);
		if (Match.getVisitorTeam().isReady()) {
		    ScaleT2.setEnabled(true);
		} else if ((Match.getVisitorTeam().isReady() == false) && ScaleT2.isEnabled() == true) {
		    ScaleT2.setEnabled(false);
		}
	    }
	});
	Pl2T2 = new JCheckBox(Match.getVisitorTeam().getGkPlayers().get(1).getName() + "(Gk, " + Match.getVisitorTeam().getGkPlayers().get(1).getHability() + ")" + " [0]");
	Pl2T2.setSize(200, 25);
	Pl2T2.setLocation(455, 130);
	Pl2T2.addActionListener(new java.awt.event.ActionListener() {

	    @Override
	    public void actionPerformed(ActionEvent e) {
		Match.getVisitorTeam().ChangeStatus(1);
		if (Match.getVisitorTeam().isReady()) {
		    ScaleT2.setEnabled(true);
		} else if ((Match.getVisitorTeam().isReady() == false) && ScaleT2.isEnabled() == true) {
		    ScaleT2.setEnabled(false);
		}
	    }
	});
	Pl3T2 = new JCheckBox(Match.getVisitorTeam().getAttackPlayers().get(0).getName() + "(Fwd, " + Match.getVisitorTeam().getAttackPlayers().get(0).getHability() + ")" + " [" + ((PlayerAt) Match.getVisitorTeam().getAttackPlayers().get(0)).getGoals() + "]");
	Pl3T2.setSize(200, 25);
	Pl3T2.setLocation(455, 155);
	Pl3T2.addActionListener(new java.awt.event.ActionListener() {

	    @Override
	    public void actionPerformed(ActionEvent e) {
		Match.getVisitorTeam().ChangeStatus(2);
		if (Match.getVisitorTeam().isReady()) {
		    ScaleT2.setEnabled(true);
		} else if ((Match.getVisitorTeam().isReady() == false) && ScaleT2.isEnabled() == true) {
		    ScaleT2.setEnabled(false);
		}
	    }
	});
	Pl4T2 = new JCheckBox(Match.getVisitorTeam().getAttackPlayers().get(1).getName() + "(Fwd, " + Match.getVisitorTeam().getAttackPlayers().get(1).getHability() + ")" + " [" + ((PlayerAt) Match.getVisitorTeam().getAttackPlayers().get(1)).getGoals() + "]");
	Pl4T2.setSize(200, 25);
	Pl4T2.setLocation(455, 180);
	Pl4T2.addActionListener(new java.awt.event.ActionListener() {

	    @Override
	    public void actionPerformed(ActionEvent e) {
		Match.getVisitorTeam().ChangeStatus(3);
		if (Match.getVisitorTeam().isReady()) {
		    ScaleT2.setEnabled(true);
		} else if ((Match.getVisitorTeam().isReady() == false) && ScaleT2.isEnabled() == true) {
		    ScaleT2.setEnabled(false);
		}
	    }
	});
	Pl5T2 = new JCheckBox(Match.getVisitorTeam().getAttackPlayers().get(2).getName() + "(Fwd, " + Match.getVisitorTeam().getAttackPlayers().get(2).getHability() + ")" + " [" + ((PlayerAt) Match.getVisitorTeam().getAttackPlayers().get(2)).getGoals() + "]");
	Pl5T2.setSize(200, 25);
	Pl5T2.setLocation(455, 205);
	Pl5T2.addActionListener(new java.awt.event.ActionListener() {

	    @Override
	    public void actionPerformed(ActionEvent e) {
		Match.getVisitorTeam().ChangeStatus(4);
		if (Match.getVisitorTeam().isReady()) {
		    ScaleT2.setEnabled(true);
		} else if ((Match.getVisitorTeam().isReady() == false) && ScaleT2.isEnabled() == true) {
		    ScaleT2.setEnabled(false);
		}
	    }
	});
	Pl6T2 = new JCheckBox(Match.getVisitorTeam().getAttackPlayers().get(3).getName() + "(Fwd, " + Match.getVisitorTeam().getAttackPlayers().get(3).getHability() + ")" + " [" + ((PlayerAt) Match.getVisitorTeam().getAttackPlayers().get(3)).getGoals() + "]");
	Pl6T2.setSize(200, 25);
	Pl6T2.setLocation(455, 230);
	Pl6T2.addActionListener(new java.awt.event.ActionListener() {

	    @Override
	    public void actionPerformed(ActionEvent e) {
		Match.getVisitorTeam().ChangeStatus(5);
		if (Match.getVisitorTeam().isReady()) {
		    ScaleT2.setEnabled(true);
		} else if ((Match.getVisitorTeam().isReady() == false) && ScaleT2.isEnabled() == true) {
		    ScaleT2.setEnabled(false);
		}
	    }
	});
	Pl7T2 = new JCheckBox(Match.getVisitorTeam().getDefensePlayers().get(0).getName() + "(Def, " + Match.getVisitorTeam().getDefensePlayers().get(0).getHability() + ")" + " [" + ((PlayerDef) Match.getVisitorTeam().getDefensePlayers().get(0)).getGoals() + "]");
	Pl7T2.setSize(200, 25);
	Pl7T2.setLocation(455, 255);
	Pl7T2.addActionListener(new java.awt.event.ActionListener() {

	    @Override
	    public void actionPerformed(ActionEvent e) {
		Match.getVisitorTeam().ChangeStatus(6);
		if (Match.getVisitorTeam().isReady()) {
		    ScaleT2.setEnabled(true);
		} else if ((Match.getVisitorTeam().isReady() == false) && ScaleT2.isEnabled() == true) {
		    ScaleT2.setEnabled(false);
		}
	    }
	});
	Pl8T2 = new JCheckBox(Match.getVisitorTeam().getDefensePlayers().get(1).getName() + "(Def, " + Match.getVisitorTeam().getDefensePlayers().get(1).getHability() + ")" + " [" + ((PlayerDef) Match.getVisitorTeam().getDefensePlayers().get(1)).getGoals() + "]");
	Pl8T2.setSize(200, 25);
	Pl8T2.setLocation(455, 280);
	Pl8T2.addActionListener(new java.awt.event.ActionListener() {

	    @Override
	    public void actionPerformed(ActionEvent e) {
		Match.getVisitorTeam().ChangeStatus(7);
		if (Match.getVisitorTeam().isReady()) {
		    ScaleT2.setEnabled(true);
		} else if ((Match.getVisitorTeam().isReady() == false) && ScaleT2.isEnabled() == true) {
		    ScaleT2.setEnabled(false);
		}
	    }
	});
	Pl9T2 = new JCheckBox(Match.getVisitorTeam().getDefensePlayers().get(2).getName() + "(Def, " + Match.getVisitorTeam().getDefensePlayers().get(2).getHability() + ")" + " [" + ((PlayerDef) Match.getVisitorTeam().getDefensePlayers().get(2)).getGoals() + "]");
	Pl9T2.setSize(200, 25);
	Pl9T2.setLocation(455, 305);
	Pl9T2.addActionListener(new java.awt.event.ActionListener() {

	    @Override
	    public void actionPerformed(ActionEvent e) {
		Match.getVisitorTeam().ChangeStatus(8);
		if (Match.getVisitorTeam().isReady()) {
		    ScaleT2.setEnabled(true);
		} else if ((Match.getVisitorTeam().isReady() == false) && ScaleT2.isEnabled() == true) {
		    ScaleT2.setEnabled(false);
		}
	    }
	});
	Pl10T2 = new JCheckBox(Match.getVisitorTeam().getDefensePlayers().get(3).getName() + "(Def, " + Match.getVisitorTeam().getDefensePlayers().get(3).getHability() + ")" + " [" + ((PlayerDef) Match.getVisitorTeam().getDefensePlayers().get(3)).getGoals() + "]");
	Pl10T2.setSize(200, 25);
	Pl10T2.setLocation(455, 330);
	Pl10T2.addActionListener(new java.awt.event.ActionListener() {

	    @Override
	    public void actionPerformed(ActionEvent e) {
		Match.getVisitorTeam().ChangeStatus(9);
		if (Match.getVisitorTeam().isReady()) {
		    ScaleT2.setEnabled(true);
		} else if ((Match.getVisitorTeam().isReady() == false) && ScaleT2.isEnabled() == true) {
		    ScaleT2.setEnabled(false);
		}
	    }
	});

//Checkboxes' background panels

	PanelTeam1 = new JPanel();
	PanelTeam1.setSize(300, 260);
	PanelTeam1.setLocation(50, 100);
	PanelTeam1.setBorder(javax.swing.BorderFactory.createBevelBorder(0));

	PanelTeam2 = new JPanel();
	PanelTeam2.setSize(300, 260);
	PanelTeam2.setLocation(450, 100);
	PanelTeam2.setBorder(javax.swing.BorderFactory.createBevelBorder(0));

	Panel3 = new JPanel();


//Adding components to the frame
	add(TitleLabel);
	add(Team1Label);
	add(Team2Label);
	add(ScaleT1);
	add(ScaleT2);
	add(Simulate);
	add(Pl1T1);
	add(Pl2T1);
	add(Pl3T1);
	add(Pl4T1);
	add(Pl5T1);
	add(Pl6T1);
	add(Pl7T1);
	add(Pl8T1);
	add(Pl9T1);
	add(Pl10T1);
	add(Pl1T2);
	add(Pl2T2);
	add(Pl3T2);
	add(Pl4T2);
	add(Pl5T2);
	add(Pl6T2);
	add(Pl7T2);
	add(Pl8T2);
	add(Pl9T2);
	add(Pl10T2);
	add(PanelTeam1);
	add(PanelTeam2);
	add(Panel3);
	addWindowListener(new windowlistener());
	setResizable(false);   //keep the size invariable
	setJMenuBar(menubar);
	setSize(800, 600);
	setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
	setVisible(true);
	AfterSimulation();

    }

    private void menuServerActionPerformed() {
	isServer = true;
	server = new Server(this);
	t1 = new Thread(server);
	t1.start();
    }

    private void menuClientActionPerformed() {
	String host = JOptionPane.showInputDialog(null, "Digite o host: ", "localhost");
	isServer = false;
	client = new Client(this, host);
	t2 = new Thread(client);
	t2.start();
    }

    private void selectModeActionPerformed() {

	Object opt[] = {"Multiplayer", "Single Player"};

	int m = JOptionPane.showOptionDialog(null, "Select the game mode:", "Mode selection", JOptionPane.YES_NO_CANCEL_OPTION, JOptionPane.QUESTION_MESSAGE, null, opt, opt[0]);

	if (m == 0) {
	    multiplayer = true;

	    Object opt2[] = {"Server", "Client"};

	    int n = JOptionPane.showOptionDialog(null, null, "Client/Server", JOptionPane.YES_NO_CANCEL_OPTION, JOptionPane.QUESTION_MESSAGE, null, opt2, opt2[0]);

	    if (n == 0) { //Server
		menuServerActionPerformed();
	    } else { //Client
		menuClientActionPerformed();
	    }

	} else {
	    multiplayer = false;
	    t1.interrupt();
	    t2.interrupt();
	}
    }

    /**
     * Sets window's components at every load
     *
     * @param T1 Team to be showed in panel 1
     * @param T2 Team to be showed in panel 2
     */
    public void setWindow(Team T1, Team T2) {
	try {
	    if (isServer && multiplayer) {
		setTitle("Server: " + T1.getName() + " x " + T2.getName() + " - Championship Simulator");
		ScaleT2.setEnabled(false);
	    } else if (!isServer && multiplayer) {
		setTitle("Client: " + T1.getName() + " x " + T2.getName() + " - Championship Simulator");
		ScaleT1.setEnabled(false);
	    } else {
		setTitle(T1.getName() + " x " + T2.getName() + " - Championship Simulator");
	    }

	    Team1Label.setText(T1.getName());
	    Team2Label.setText(T2.getName());

	    if (T1.isReady()) {
		ScaleT1.setEnabled(true);
	    } else {
		ScaleT1.setEnabled(false);
	    }
	    if (T2.isReady()) {
		ScaleT2.setEnabled(true);
	    } else {
		ScaleT2.setEnabled(false);
	    }
	    if (T1.isReady() && T2.isReady()) {
		Simulate.setEnabled(true);
	    } else {
		Simulate.setEnabled(false);
	    }

//Ckeckboxes refering to team 1

	    Pl1T1.setText(Match.getHomeTeam().getGkPlayers().get(0).getName() + "(Gk, " + Match.getHomeTeam().getGkPlayers().get(0).getHability() + ")" + " [0]");
	    Pl2T1.setText(Match.getHomeTeam().getGkPlayers().get(1).getName() + "(Gk, " + Match.getHomeTeam().getGkPlayers().get(1).getHability() + ")" + " [0]");
	    Pl3T1.setText(Match.getHomeTeam().getDefensePlayers().get(0).getName() + "(Def, " + Match.getHomeTeam().getDefensePlayers().get(0).getHability() + ")" + " [" + ((PlayerDef) Match.getHomeTeam().getDefensePlayers().get(0)).getGoals() + "]");
	    Pl4T1.setText(Match.getHomeTeam().getDefensePlayers().get(1).getName() + "(Def, " + Match.getHomeTeam().getDefensePlayers().get(1).getHability() + ")" + " [" + ((PlayerDef) Match.getHomeTeam().getDefensePlayers().get(1)).getGoals() + "]");
	    Pl5T1.setText(Match.getHomeTeam().getDefensePlayers().get(2).getName() + "(Def, " + Match.getHomeTeam().getDefensePlayers().get(2).getHability() + ")" + " [" + ((PlayerDef) Match.getHomeTeam().getDefensePlayers().get(2)).getGoals() + "]");
	    Pl6T1.setText(Match.getHomeTeam().getDefensePlayers().get(3).getName() + "(Def, " + Match.getHomeTeam().getDefensePlayers().get(3).getHability() + ")" + " [" + ((PlayerDef) Match.getHomeTeam().getDefensePlayers().get(3)).getGoals() + "]");
	    Pl7T1.setText(Match.getHomeTeam().getAttackPlayers().get(0).getName() + "(Fwd, " + Match.getHomeTeam().getAttackPlayers().get(0).getHability() + ")" + " [" + ((PlayerAt) Match.getHomeTeam().getAttackPlayers().get(0)).getGoals() + "]");
	    Pl8T1.setText(Match.getHomeTeam().getAttackPlayers().get(1).getName() + "(Fwd, " + Match.getHomeTeam().getAttackPlayers().get(1).getHability() + ")" + " [" + ((PlayerAt) Match.getHomeTeam().getAttackPlayers().get(1)).getGoals() + "]");
	    Pl9T1.setText(Match.getHomeTeam().getAttackPlayers().get(2).getName() + "(Fwd, " + Match.getHomeTeam().getAttackPlayers().get(2).getHability() + ")" + " [" + ((PlayerAt) Match.getHomeTeam().getAttackPlayers().get(2)).getGoals() + "]");
	    Pl10T1.setText(Match.getHomeTeam().getAttackPlayers().get(3).getName() + "(Fwd, " + Match.getHomeTeam().getAttackPlayers().get(3).getHability() + ")" + " [" + ((PlayerAt) Match.getHomeTeam().getAttackPlayers().get(3)).getGoals() + "]");


//CheckBoxes refering to team 2

	    Pl1T2.setText(Match.getVisitorTeam().getGkPlayers().get(0).getName() + "(Gk, " + Match.getVisitorTeam().getGkPlayers().get(0).getHability() + ")" + " [0]");
	    Pl2T2.setText(Match.getVisitorTeam().getGkPlayers().get(1).getName() + "(Gk, " + Match.getVisitorTeam().getGkPlayers().get(1).getHability() + ")" + " [0]");
	    Pl3T2.setText(Match.getVisitorTeam().getDefensePlayers().get(0).getName() + "(Def, " + Match.getVisitorTeam().getDefensePlayers().get(0).getHability() + ")" + " [" + ((PlayerDef) Match.getVisitorTeam().getDefensePlayers().get(0)).getGoals() + "]");
	    Pl4T2.setText(Match.getVisitorTeam().getDefensePlayers().get(1).getName() + "(Def, " + Match.getVisitorTeam().getDefensePlayers().get(1).getHability() + ")" + " [" + ((PlayerDef) Match.getVisitorTeam().getDefensePlayers().get(1)).getGoals() + "]");
	    Pl5T2.setText(Match.getVisitorTeam().getDefensePlayers().get(2).getName() + "(Def, " + Match.getVisitorTeam().getDefensePlayers().get(2).getHability() + ")" + " [" + ((PlayerDef) Match.getVisitorTeam().getDefensePlayers().get(2)).getGoals() + "]");
	    Pl6T2.setText(Match.getVisitorTeam().getDefensePlayers().get(3).getName() + "(Def, " + Match.getVisitorTeam().getDefensePlayers().get(3).getHability() + ")" + " [" + ((PlayerDef) Match.getVisitorTeam().getDefensePlayers().get(3)).getGoals() + "]");
	    Pl7T2.setText(Match.getVisitorTeam().getAttackPlayers().get(0).getName() + "(Fwd, " + Match.getVisitorTeam().getAttackPlayers().get(0).getHability() + ")" + " [" + ((PlayerAt) Match.getVisitorTeam().getAttackPlayers().get(0)).getGoals() + "]");
	    Pl8T2.setText(Match.getVisitorTeam().getAttackPlayers().get(1).getName() + "(Fwd, " + Match.getVisitorTeam().getAttackPlayers().get(1).getHability() + ")" + " [" + ((PlayerAt) Match.getVisitorTeam().getAttackPlayers().get(1)).getGoals() + "]");
	    Pl9T2.setText(Match.getVisitorTeam().getAttackPlayers().get(2).getName() + "(Fwd, " + Match.getVisitorTeam().getAttackPlayers().get(2).getHability() + ")" + " [" + ((PlayerAt) Match.getVisitorTeam().getAttackPlayers().get(2)).getGoals() + "]");
	    Pl10T2.setText(Match.getVisitorTeam().getAttackPlayers().get(3).getName() + "(Fwd, " + Match.getVisitorTeam().getAttackPlayers().get(3).getHability() + ")" + " [" + ((PlayerAt) Match.getVisitorTeam().getAttackPlayers().get(3)).getGoals() + "]");

	} catch (java.lang.NullPointerException ex) {
	    ReadingError();
	}
    }

    /**
     * Shows the authors names and USP numbers
     */
    private void menuAboutActionPerformed() {
	JOptionPane.showMessageDialog(null, "Authors:\nJosé Filipe M Santos \t- 7277502\nMatheus M. Teixeira \t- 7277482", "About", JOptionPane.INFORMATION_MESSAGE);
    }

    /**
     * Lock first panel's the checkboxes, requisite to simulate the game
     */
    public void ScaleT1ActionPerformed() {
	if (ScaleT1.getText().equalsIgnoreCase("Scale")) {
//	    JOptionPane.showMessageDialog(null, "Scaled", "Team Sclaed", JOptionPane.INFORMATION_MESSAGE);
	    if (isServer && multiplayer) {
		server.sendData((Object) Match.getHomeTeam());
		server.sendData((Object) Match.getHomeTeam().getStarting());
	    }

	    ScaleT1.setText("Change");
	    ScaleT1.setToolTipText("Click here to change the team's formation");
	    Pl1T1.setEnabled(false);
	    Pl2T1.setEnabled(false);
	    Pl3T1.setEnabled(false);
	    Pl4T1.setEnabled(false);
	    Pl5T1.setEnabled(false);
	    Pl6T1.setEnabled(false);
	    Pl7T1.setEnabled(false);
	    Pl8T1.setEnabled(false);
	    Pl9T1.setEnabled(false);
	    Pl10T1.setEnabled(false);
	    if (ScaleT2.getText().equalsIgnoreCase("Change")) {
		Simulate.setEnabled(true);
	    }

	} else if (ScaleT1.getText().equalsIgnoreCase("Change")) {
	    ScaleT1.setText("Scale");
	    ScaleT1.setToolTipText("Click here to select the current players");
	    Pl1T1.setEnabled(true);
	    Pl2T1.setEnabled(true);
	    Pl3T1.setEnabled(true);
	    Pl4T1.setEnabled(true);
	    Pl5T1.setEnabled(true);
	    Pl6T1.setEnabled(true);
	    Pl7T1.setEnabled(true);
	    Pl8T1.setEnabled(true);
	    Pl9T1.setEnabled(true);
	    Pl10T1.setEnabled(true);
	    Simulate.setEnabled(false);
	}
    }

    /**
     * Lock the second panel's cheboxes, requisite to simulate the game
     */
    public void ScaleT2ActionPerformed() {
	if (ScaleT2.getText().equalsIgnoreCase("Scale")) {
//	    JOptionPane.showMessageDialog(null, "Scaled", "Team Sclaed", JOptionPane.INFORMATION_MESSAGE);
	    if (!isServer && multiplayer) {
		client.sendData((Object) Match.getVisitorTeam());
		client.sendData((Object) Match.getVisitorTeam().getStarting());
	    }
	    ScaleT2.setText("Change");
	    ScaleT2.setToolTipText("Click here to change the team's formation");
	    Pl1T2.setEnabled(false);
	    Pl2T2.setEnabled(false);
	    Pl3T2.setEnabled(false);
	    Pl4T2.setEnabled(false);
	    Pl5T2.setEnabled(false);
	    Pl6T2.setEnabled(false);
	    Pl7T2.setEnabled(false);
	    Pl8T2.setEnabled(false);
	    Pl9T2.setEnabled(false);
	    Pl10T2.setEnabled(false);
	    if (ScaleT1.getText().equalsIgnoreCase("Change")) {
		Simulate.setEnabled(true);
	    }
	} else if (ScaleT2.getText().equalsIgnoreCase("Change")) {
	    ScaleT2.setText("Scale");
	    ScaleT2.setToolTipText("Click here to select the current players");
	    Pl1T2.setEnabled(true);
	    Pl2T2.setEnabled(true);
	    Pl3T2.setEnabled(true);
	    Pl4T2.setEnabled(true);
	    Pl5T2.setEnabled(true);
	    Pl6T2.setEnabled(true);
	    Pl7T2.setEnabled(true);
	    Pl8T2.setEnabled(true);
	    Pl9T2.setEnabled(true);
	    Pl10T2.setEnabled(true);
	    Simulate.setEnabled(false);
	}
    }

    /**
     * Load data from file
     */
    private void LoadActionPerformed() {
	JFileChooser filechooser = new JFileChooser();
	FileFilter filter = new FileNameExtensionFilter("Simulator File", "txt");
	filechooser.setFileFilter(filter);
	int returnval = filechooser.showOpenDialog(this);
	if (returnval == JFileChooser.APPROVE_OPTION) {
	    GameData N = new GameData();
	    N.loadFile(filechooser.getSelectedFile().getPath());
	    this.G = N;
	    this.Match = new Game(new Date(), N.getLoadedTeam1(), N.getLoadedTeam2());
	    setWindow(this.Match.getHomeTeam(), this.Match.getVisitorTeam());
	}
	Pl1T1.setEnabled(true);
	Pl2T1.setEnabled(true);
	Pl3T1.setEnabled(true);
	Pl4T1.setEnabled(true);
	Pl5T1.setEnabled(true);
	Pl6T1.setEnabled(true);
	Pl7T1.setEnabled(true);
	Pl8T1.setEnabled(true);
	Pl9T1.setEnabled(true);
	Pl10T1.setEnabled(true);

	Pl1T2.setEnabled(true);
	Pl2T2.setEnabled(true);
	Pl3T2.setEnabled(true);
	Pl4T2.setEnabled(true);
	Pl5T2.setEnabled(true);
	Pl6T2.setEnabled(true);
	Pl7T2.setEnabled(true);
	Pl8T2.setEnabled(true);
	Pl9T2.setEnabled(true);
	Pl10T2.setEnabled(true);

	Pl1T1.setSelected(false);
	Pl2T1.setSelected(false);
	Pl3T1.setSelected(false);
	Pl4T1.setSelected(false);
	Pl5T1.setSelected(false);
	Pl6T1.setSelected(false);
	Pl7T1.setSelected(false);
	Pl8T1.setSelected(false);
	Pl9T1.setSelected(false);
	Pl10T1.setSelected(false);

	Pl1T2.setSelected(false);
	Pl2T2.setSelected(false);
	Pl3T2.setSelected(false);
	Pl4T2.setSelected(false);
	Pl5T2.setSelected(false);
	Pl6T2.setSelected(false);
	Pl7T2.setSelected(false);
	Pl8T2.setSelected(false);
	Pl9T2.setSelected(false);
	Pl10T2.setSelected(false);


	ScaleT1.setText("Scale");
	ScaleT2.setText("Scale");




    }

    /**
     * Opens the dialog box and asks if the user wants to save before quitting
     */
    private void QuitActionPerformed() {

	int ans = JOptionPane.showConfirmDialog(null, "Would you like to save?", "Save Before ?", JOptionPane.YES_NO_CANCEL_OPTION);
	if (ans == JOptionPane.YES_OPTION) {
	    JFileChooser filechooser = new JFileChooser();
	    FileFilter filter = new FileNameExtensionFilter("Simulator File", "txt");
	    filechooser.setFileFilter(filter);

	    int returnval = filechooser.showSaveDialog(this);
	    if (returnval == JFileChooser.APPROVE_OPTION) {
		this.G.writeFile(filechooser.getSelectedFile().getPath() + ".txt", Match);
		System.exit(0);

	    }

	} else if (ans == JOptionPane.NO_OPTION) {
	    System.exit(0);
	}



    }

    /**
     * Saves the current game data in a file
     */
    private void SaveActionPerformed() {

	JFileChooser filechooser = new JFileChooser();
	FileFilter filter = new FileNameExtensionFilter("Simulator File", "txt");
	filechooser.setFileFilter(filter);

	int returnval = filechooser.showSaveDialog(this);
	if (returnval == JFileChooser.APPROVE_OPTION) {
	    this.G.writeFile(filechooser.getSelectedFile().getPath() + ".txt", Match);
	}


    }

    /**
     * Set the window apropriately after the simulation
     */
    public void AfterSimulation() {

	setWindow(Match.getHomeTeam(), Match.getVisitorTeam());

	Pl1T1.setSelected(Match.getHomeTeam().isStarting(0));
	Pl2T1.setSelected(Match.getHomeTeam().isStarting(1));
	Pl3T1.setSelected(Match.getHomeTeam().isStarting(2));
	Pl4T1.setSelected(Match.getHomeTeam().isStarting(3));
	Pl5T1.setSelected(Match.getHomeTeam().isStarting(4));
	Pl6T1.setSelected(Match.getHomeTeam().isStarting(5));
	Pl7T1.setSelected(Match.getHomeTeam().isStarting(6));
	Pl8T1.setSelected(Match.getHomeTeam().isStarting(7));
	Pl9T1.setSelected(Match.getHomeTeam().isStarting(8));
	Pl10T1.setSelected(Match.getHomeTeam().isStarting(9));

	Pl1T2.setSelected(Match.getVisitorTeam().isStarting(0));
	Pl2T2.setSelected(Match.getVisitorTeam().isStarting(1));
	Pl3T2.setSelected(Match.getVisitorTeam().isStarting(2));
	Pl4T2.setSelected(Match.getVisitorTeam().isStarting(3));
	Pl5T2.setSelected(Match.getVisitorTeam().isStarting(4));
	Pl6T2.setSelected(Match.getVisitorTeam().isStarting(5));
	Pl7T2.setSelected(Match.getVisitorTeam().isStarting(6));
	Pl8T2.setSelected(Match.getVisitorTeam().isStarting(7));
	Pl9T2.setSelected(Match.getVisitorTeam().isStarting(8));
	Pl10T2.setSelected(Match.getVisitorTeam().isStarting(9));
    }

    /**
     * Simulates the game and show the score
     */
    private void SimulateActionPerformed() {

	Match = new Game(new Date(), Match.getHomeTeam(), Match.getVisitorTeam());
	Match.simulateGame();
	JOptionPane.showMessageDialog(null, "Score: " + Match.getscore(), "Game Score", JOptionPane.INFORMATION_MESSAGE);
	AfterSimulation();
    }

    /**
     * Show history of team 1
     */
    private void Ht1ActionPerformed() {

	try {
	    JOptionPane.showMessageDialog(null, Match.getHomeTeam().getResults(), Match.getHomeTeam().getName() + " History", JOptionPane.INFORMATION_MESSAGE);
	} catch (java.lang.NullPointerException ex) {
	    JOptionPane.showMessageDialog(null, Match.getHomeTeam().getResults(), Match.getHomeTeam().getName() + " History", JOptionPane.INFORMATION_MESSAGE);
	}
    }

    /**
     * Show the history of team 2
     */
    private void Ht2ActionPerformed() {

	try {
	    JOptionPane.showMessageDialog(null, Match.getVisitorTeam().getResults(), Match.getVisitorTeam().getName() + " History", JOptionPane.INFORMATION_MESSAGE);
	} catch (java.lang.NullPointerException ex) {
	    JOptionPane.showMessageDialog(null, Match.getVisitorTeam().getResults(), Match.getVisitorTeam().getName() + " History", JOptionPane.INFORMATION_MESSAGE);
	}
    }

    /**
     * Treats reading error exceptions
     */
    private void ReadingError() {
	JOptionPane.showMessageDialog(null, "I/O Error - Wrong format", "Error", JOptionPane.ERROR_MESSAGE);
	LoadActionPerformed();
    }

    public static void main(String args[]) {

	GameData db = new GameData();
	try {
	    db.loadFile(System.getProperty("user.dir") + "/Dados.txt");
	    Window w = new Window(db);
	} catch (java.lang.NullPointerException ex) {
	    JOptionPane.showMessageDialog(null, "No Sample File Found, make sure you have with right format Dados.txt in " + System.getProperty("user.dir"), "Error", JOptionPane.ERROR_MESSAGE);
	}


    }
}
